<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Graph
 * 
 * Show the graph page for an object.
 */
class escape_repositoryui_action_Graph extends escape_repositoryui_Action
{
	public function handleRequest()
	{
		/* @var $repository escape_repository_Repository */
		$repository =& $this->requestAttributes['repository'];
		/* @var $repositoryUi escape_repositoryui_RepositoryUi */
		$repositoryUi =& $this->requestAttributes['repositoryUi'];

		// fetch the ID from the request
		$objectId = $_REQUEST['actionValue'];
		$objectUri = $repository->convertDataObjectIdToObjectUri($objectId);
		
		$repositorySchema =& $repository->repositorySchema;
		
		$aggregatedResource = null;
		$aggregation = null;
		try {
			$oreObject = $repository->getOreObjectByUri($objectUri);
			$this->requestAttributes['oreObject'] =& $oreObject;
		}
		catch(Exception $exception)
		{
			//TODO: 404 when object does not exist?
			$repositoryUi->displayErrorPage($exception->getMessage());
			return null;
		}
		if($oreObject instanceof escape_repository_OreAggregatedResource)
		{
			$aggregatedResource = $oreObject;
			$aggregation = $aggregatedResource->getAggregation();
		}
		else if($oreObject instanceof escape_repository_OreAggregation)
		{
			$aggregation = $oreObject;
		}
		$this->requestAttributes['aggregation'] =& $aggregation;
		$aggregationUrl = $aggregation->getExternalUri();
		$remUrl = $aggregation->getRemExternalUri();
	
		$repositoryUi->setTemplateFromAggregation($aggregation);
		
		// is there any filter specified?
		$rdfTypes = null;
		if(isset($_REQUEST['filter']))
		{
			$rdfTypes = $_REQUEST['filter'];
			if(!is_array($rdfTypes))
			{
				$rdfTypes = array($rdfTypes);
			}
		}
		
		// add filters to the breadcrub title
		$breadCrumbTitle = $oreObject->getTitle();
		if($breadCrumbTitle === null || $breadCrumbTitle === "")
		{
			$breadCrumbTitle = 'Unnamed object ' . $oreObject->getId();
		}
		if($rdfTypes !== null && count($rdfTypes) > 0)
		{
			$rdfTypeLabels = array();
			foreach($rdfTypes as $rdfType)
			{
				$rdfTypeLabels[] = $repositorySchema->getResourceLabelPlural($rdfType);
			}
			
			$breadCrumbTitle .= ' (' . implode(' / ', $rdfTypeLabels) . ')'; 
		}
	
		// add a breadcrumb for this object
		$breadCrumb = new escape_repositoryui_BreadCrumb();
		$breadCrumb->setTitle($breadCrumbTitle);
		$breadCrumb->setUrl($_SERVER['REQUEST_URI']);
		$repositoryUi->addBreadCrumb($breadCrumb);
	
		// add link to aggregation to headers
		header('Link: <' . $aggregationUrl . '>; rel="aggregation"', true);
		// add link to resource map to headers
		header('Link: <' . $remUrl . '>; type="application/rdf+xml"; rel="resourcemap"', false);
		
		return 'GraphShow';
	}
}